<?php
include_once 'header_php.php';
$post_query=$_POST['query'];
$sql=NULL;
if($_POST['type']=='membre'){
    $champ=$_POST['champ'];
    $sql='SELECT login, nom, prenom FROM Personne WHERE minimizetext('.$champ.') LIKE \'%\'|| minimizetext(\''.$post_query.'\') || \'%\' ORDER BY nom ;';
}
else{
    $sql='SELECT nom FROM Association WHERE minimizetext(nom) LIKE \'%\'|| minimizetext(\''.$post_query.'\') || \'%\' ORDER BY nom ;';
}
$query=new DbQuery($db,$sql);
$query->SendQuery();
$results=array();
while($line=$query->GetResult()){
    $results[]=$line;
}
$title=$post_query.' - Recherche';
include_once 'header_html.php';
?>
<div class="wrapper">
    <div class="container span8 offset2 ">
        <?php if(count($results)>0){?>
        <h1>Résultats</h1>
            <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>Nom</th>
                        <?php if($_POST['type']=='membre'){ ?>
                        <th>Prénom</th>
                        <th>Login</th>
                        <?php } ?>
                    </tr>
                    </thead>
                    <tbody>
                        
                        <?php 
                        $i=0;
                        foreach ($results as $result) {
                                echo "<tr>\n";
                                echo "<td>".++$i."\n";
                                
                                if($_POST['type']=='membre'){
                                    echo "<td><a href=\"profil.php?login=".$result['login']."\">".$result['nom']."</a></td>\n";
                                    echo "<td><a href=\"profil.php?login=".$result['login']."\">".$result['prenom']."</a></td>\n";
                                    echo "<td><a href=\"profil.php?login=".$result['login']."\">".$result['login']."</a></td>\n";
                                }
                                else{
                                    echo "<td><a href=\"asso.php?nom=".$result['nom']."\">".$result['nom']."</a></td>\n";
                                }
                                echo "<td>\n";
                        } ?>
                    </tbody>
            </table>
        <?php }else {   
          echo '<div class="alert alert-danger">Aucun résultat ne correspond à votre recherche</div>';
        
          } ?>
    </div>
</div>
